<html>
<head><meta charset="utf-8"><title>Recursive cow enum · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html">Recursive cow enum</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235854504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235854504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235854504">(Apr 23 2021 at 14:59)</a>:</h4>
<p>At some point in the past I tried this code:</p>
<div class="codehilite"><pre><span></span><code>use std::borrow::Cow;

#[derive(Clone)]
enum Test &lt;&#39;a&gt; {
    Int(u8),
    Array(Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;),
}
</code></pre></div>
<p>Playground: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=cc45c60985543b19460d6566dbf45caa">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=cc45c60985543b19460d6566dbf45caa</a></p>
<p>But it does not work and fails with surprising errors like:</p>
<div class="codehilite"><pre><span></span><code>error[E0277]: the trait bound `[Test&lt;&#39;a&gt;]: ToOwned` is not satisfied in `Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;`
 --&gt; src/lib.rs:6:11
  |
6 |     Array(Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;),
  |           ^^^^^^^^^^^^^^^^^^^ within `Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;`, the trait `ToOwned` is not implemented for `[Test&lt;&#39;a&gt;]`
  |
  = help: the following implementations were found:
            &lt;[T] as ToOwned&gt;
  = note: required because it appears within the type `Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;`
  = note: no field of an enum variant may have a dynamically sized type
  = help: change the field&#39;s type to have a statically known size
help: borrowed types always have a statically known size
  |
6 |     Array(&amp;Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;),
  |           ^
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
6 |     Array(Box&lt;Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;&gt;),
  |           ^^^^                   ^
</code></pre></div>
<p>Whereas <code>ToOwned</code> <em>should</em> implemented for any <code>[T]</code> providing that <code>T: Clone</code></p>



<a name="235854929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235854929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235854929">(Apr 23 2021 at 15:00)</a>:</h4>
<p>Especially the node around <code>no field of an enum variant may have a dynamically sized type</code> seems weird given that there is DST involved here</p>



<a name="235855416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235855416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235855416">(Apr 23 2021 at 15:02)</a>:</h4>
<p>Cow definitions is</p>
<div class="codehilite"><pre><span></span><code>pub enum Cow&lt;&#39;a, B&gt; where
    B: &#39;a + ToOwned + ?Sized,  {
    Borrowed(&amp;&#39;a B),
    Owned(&lt;B as ToOwned&gt;::Owned),
}
</code></pre></div>
<p>So its either a reference to [Test&lt;'a&gt;] (which should be Sized) or a Vec&lt;Test&lt;'a&gt;&gt; (which should Sized too)</p>



<a name="235869395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235869395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235869395">(Apr 23 2021 at 16:25)</a>:</h4>
<p>i think we end up having a mixed cycle here, where some of the predicates are coinductive but not all <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> this issue is the reason i've asked about mixed coinductive cycles before</p>



<a name="235869480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235869480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235869480">(Apr 23 2021 at 16:25)</a>:</h4>
<p>there might be a different solution to this, but looking at for how long this issue has been open, that one might be hard and not something i can say anything about rn</p>



<a name="235870243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235870243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235870243">(Apr 23 2021 at 16:31)</a>:</h4>
<p>i think this is <a href="https://github.com/rust-lang/rust/issues/47032">#47032</a></p>



<a name="235875087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235875087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235875087">(Apr 23 2021 at 17:04)</a>:</h4>
<p>I originally posted this 2 years ago in <a href="https://github.com/rust-lang/rust/issues/23707">https://github.com/rust-lang/rust/issues/23707</a><br>
But I guess there are multiple issues for this</p>



<a name="235875662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235875662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235875662">(Apr 23 2021 at 17:08)</a>:</h4>
<p>Would making Sized codinductive help here?</p>



<a name="235876109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235876109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235876109">(Apr 23 2021 at 17:11)</a>:</h4>
<p>not on it's own sadly</p>



<a name="235876149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235876149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235876149">(Apr 23 2021 at 17:11)</a>:</h4>
<p>because the cycle is like <code>A: Sized -&gt; B: ToOwned -&gt; C: Sized -&gt; A: Sized</code> and <code>ToOwned</code> is not coinductive</p>



<a name="235876241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/Recursive%20cow%20enum/near/235876241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/Recursive.20cow.20enum.html#235876241">(Apr 23 2021 at 17:12)</a>:</h4>
<p>so even if <code>Sized</code> on its own ends up being coinductive this cycle would still cause an error</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>